using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data.OleDb;

/// <summary>
/// Summary description for BdcWssFieldType
/// </summary>
namespace Siderys.Blog.CustomField
{
    public class BdcWssFieldType : BaseFieldControl
    {
        protected DropDownList ddlResultSelect;
        private OleDbConnection mConnection = null;
        private OleDbCommand mCommand = null;
        private OleDbDataReader mReader = null;

        public BdcWssFieldType()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        protected override string DefaultTemplateName
        {
            get
            {
                return "BdcWss";
            }
        }

        public override object Value
        {
            get
            {
                return ddlResultSelect.SelectedItem.Text;
            }

            set
            {
                EnsureChildControls();
                ddlResultSelect.Items.FindByText(value.ToString()).Selected = true;
            }
        }

        public override void Focus()
        {
            EnsureChildControls();
            ddlResultSelect.Focus();
        }

        private void FillDropDownListFromDataBase()
        {
            try
            {
                //obtengo los valores para la conexion y la consulta almacenados en las propiedades del campo.
                string lConnectionString = this.Field.GetCustomProperty("StringConnection").ToString();
                string lSelectString = this.Field.GetCustomProperty("StringSelect").ToString();

                mConnection = new OleDbConnection(lConnectionString);
                mConnection.Open();
                mCommand = new OleDbCommand(lSelectString, mConnection);
                mReader = mCommand.ExecuteReader();
                ddlResultSelect.Items.Clear();
                while (mReader.Read())
                {
                    ddlResultSelect.Items.Add(new ListItem(mReader.GetString(0)));
                }               
            }
            catch (OleDbException ex)
            {
                throw ex;
            }
            finally
            {
                mReader.Close();
                mConnection.Close();
            }
        }

        protected override void CreateChildControls()
        {
            if (this.Field == null) return;
            base.CreateChildControls();
            if (this.ControlMode == Microsoft.SharePoint.WebControls.SPControlMode.Display)
                return;
            ddlResultSelect = (DropDownList)(TemplateContainer.FindControl("ddlResultSelect"));
            if (ddlResultSelect == null)
            {
                throw new ArgumentException("DropDowList es nulo.......");
            }

            if (!Page.IsPostBack)
            {
                try
                {
                    FillDropDownListFromDataBase();
                }
                catch (Exception ex)
                {
                    ddlResultSelect.Items.Add(ex.Message);
                }
            }
        }
    }
}